/*
 * Copyright (c) 2014 Luca Agostini, Alessandro Cantini, Alessandro Dignani
 *
 * This file is part of MeteoCal.
 *
 * MeteoCal is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package it.polimi.se2.agostini_cantini_dignani.meteocal.gui;

import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.Calendar;
import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.Event;
import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.User;
import it.polimi.se2.agostini_cantini_dignani.meteocal.entity.WeatherForecast;
import org.primefaces.model.ScheduleEvent;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Alessandro Cantini <alessandro.cantini@mail.polimi.it>
 */
public class ScheduleEventImpl implements ScheduleEvent {

	private final boolean editPermission;
	private final boolean viewPermission;
	private Event event;
	private Date start;
	private Date end;

	public ScheduleEventImpl(Calendar calendar, Event event, User loggedUser) {
		if (calendar == null || event == null || loggedUser == null) {
			throw new IllegalArgumentException();
		}
		boolean loggedUserIsCalendarOwner = loggedUser.equals(calendar.getOwner());
		this.event = event;
		editPermission = loggedUser.equals(event.getOwner());
		viewPermission = loggedUserIsCalendarOwner || event.isPublic();
		updateDates(event);
	}

	public Integer getEventId() {
		return event.getEventId();
	}

	public List<User> getInvitedUsers() {
		if (!viewPermission) {
			return Collections.emptyList();
		}
		return event.getInvitedUsers();
	}

	public List<User> getUsersWhoHaveDeclined() {
		if (!viewPermission) {
			return Collections.emptyList();
		}
		return event.getUsersWhoHaveDeclined();
	}

	public List<Calendar> getParticipantCalendars() {
		if (!viewPermission) {
			return Collections.emptyList();
		}
		return event.getParticipantCalendars();
	}

	public WeatherForecast getWeatherForecast() {
		if (!viewPermission) {
			return null;
		}
		return event.getWeatherForecast();
	}

	public User getOwner() {
		if (!viewPermission) {
			return null;
		}
		return event.getOwner();
	}

	public boolean isOutdoor() {
		if (!viewPermission) {
			return false;
		}
		return event.isOutdoor();
	}

	public boolean isPublic() {
		return event.isPublic();
	}

	public String getPlace() {
		if (!viewPermission) {
			return "";
		}
		return event.getPlace();
	}

	@Override
	public String getId() {
		return String.valueOf(event.getEventId());
	}

	@Override
	public void setId(String id) {
		throw new UnsupportedOperationException("Not supported.");
	}

	@Override
	public Object getData() {
		return null;
	}

	@Override
	public String getTitle() {
		if (!viewPermission) {
			return "";
		}
		return event.getTitle();
	}

	@Override
	public Date getStartDate() {
		return start;
	}

	@Override
	public Date getEndDate() {
		return end;
	}

	@Override
	public boolean isAllDay() {
		return false;
	}

	@Override
	public String getStyleClass() {
		return null;
	}

	@Override
	public boolean isEditable() {
		return editPermission;
	}

	@Override
	public String getDescription() {
		if (!viewPermission) {
			return "";
		}
		return event.getDescription();
	}

	public void updateEvent(Event event) {
		this.event = event;
		updateDates(event);
	}

	private void updateDates(Event e) {
		start = new Date(e.getStartTime().getTime());
		end = new Date((e.getEndTime().getTime()));
	}
}
